package com.edu.exam.enums.grading;

import com.edu.exam.enums.core.BaseEnum;

/**
 * 成绩操作结果枚举
 *
 * <p>管理成绩记录的操作结果状态，包括成功、失败、部分成功、跳过、待处理等结果。</p>
 *
 * @author System
 * @version 1.0.0
 * @since 2025-11-14
 */
public enum GradeOperationResult implements BaseEnum {

    /**
     * 成功
     */
    SUCCESS("SUCCESS", "成功"),

    /**
     * 失败
     */
    FAILED("FAILED", "失败"),

    /**
     * 部分成功
     */
    PARTIAL("PARTIAL", "部分成功"),

    /**
     * 跳过
     */
    SKIPPED("SKIPPED", "跳过"),

    /**
     * 待处理
     */
    PENDING("PENDING", "待处理"),

    /**
     * 超时
     */
    TIMEOUT("TIMEOUT", "超时"),

    /**
     * 取消
     */
    CANCELLED("CANCELLED", "取消"),

    /**
     * 重试
     */
    RETRY("RETRY", "重试"),

    /**
     * 验证失败
     */
    VALIDATION_FAILED("VALIDATION_FAILED", "验证失败"),

    /**
     * 权限不足
     */
    PERMISSION_DENIED("PERMISSION_DENIED", "权限不足"),

    /**
     * 业务规则冲突
     */
    BUSINESS_RULE_VIOLATION("BUSINESS_RULE_VIOLATION", "业务规则冲突");

    private final String code;
    private final String description;

    GradeOperationResult(String code, String description) {
        this.code = code;
        this.description = description;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /**
     * 根据编码查找枚举值
     *
     * @param code 业务编码
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果编码无效
     */
    public static GradeOperationResult fromCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            throw new IllegalArgumentException("Grade operation result code cannot be null or empty");
        }

        for (GradeOperationResult result : values()) {
            if (result.getCode().equals(code)) {
                return result;
            }
        }
        throw new IllegalArgumentException("Unknown grade operation result code: " + code);
    }

    /**
     * 根据描述查找枚举值
     *
     * @param description 显示描述
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果描述无效
     */
    public static GradeOperationResult fromDescription(String description) {
        if (description == null || description.trim().isEmpty()) {
            throw new IllegalArgumentException("Grade operation result description cannot be null or empty");
        }

        for (GradeOperationResult result : values()) {
            if (result.getDescription().equals(description)) {
                return result;
            }
        }
        throw new IllegalArgumentException("Unknown grade operation result description: " + description);
    }

    @Override
    public String toString() {
        return this.code + "(" + this.description + ")";
    }
}